package com.qqtech.qquan.circle.service;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.exception.BizzException;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.circle.dao.CircleBaseDao;
import com.qqtech.qquan.circle.dao.CircleInviteDao;
import com.qqtech.qquan.circle.dao.CircleMemberDao;
import com.qqtech.qquan.circle.enums.CircleAuditStatusEnum;
import com.qqtech.qquan.circle.enums.CircleInviteStatusDictEnum;
import com.qqtech.qquan.circle.model.CircleBase;
import com.qqtech.qquan.circle.model.CircleInvite;
import com.qqtech.qquan.circle.model.CircleInviteQuery;
import com.qqtech.qquan.push.service.PushBizzService;

@Service
public class CircleInviteServiceImpl extends BaseServiceImpl<CircleInvite>implements CircleInviteService {
	public static final Logger log = LoggerFactory.getLogger(CircleInviteServiceImpl.class);

	@Resource
	private CircleInviteDao circleInviteDao;
	@Resource
	private CircleMemberDao circleMemberDao;
	@Resource
	private CircleMemberService circleMemberService;
	@Resource
	private CircleBaseDao circleBaseDao;
	@Resource
	private PushBizzService pushBizzService;

	@Override
	protected BaseDao<CircleInvite> getBaseDao() {
		return circleInviteDao;
	}

	@Override
	public int addCircleInvite(CircleInvite circleInvite) throws BizzException {
		if (circleInvite == null) {
			throw new BizzException("圈子邀请信息不能为空！");
		}
		Integer inviterId = circleInvite.getInviterId();// 邀请人
		Integer beInviterId = circleInvite.getBeInviterId();// 被邀请人
		Integer circleId = circleInvite.getCircleId();// 圈子ID
		if (inviterId == null) {
			throw new BizzException("邀请人为空！");
		}
		if (beInviterId == null) {
			throw new BizzException("被邀请人为空！");
		}
		if (circleId == null) {
			throw new BizzException("圈子id为空！");
		}
		CircleBase circle = circleBaseDao.getById(circleId);
		if (circle == null) {
			throw new BizzException("找不到这个圈子!");
		}
		if (circle.getAuditStatus() != CircleAuditStatusEnum.AUDIT_PASS.getCode()) {
			throw new BizzException("该圈子未审核通过,不能邀请成员!");
		}
		if (!circleMemberService.isCircleMember(inviterId, circleId)) {
			throw new BizzException("您不是该圈子成员,不能邀请!");
		}
		if (isCircleInviteExist(inviterId, beInviterId, circleId)) {
			throw new BizzException("您已经提交了邀请,不能重复操作！");
		}

		if (circleMemberService.isCircleMember(beInviterId, circleId)) {
			throw new BizzException("他已经是这个圈子的成员！");
		}
		circleInvite.setStatus(CircleInviteStatusDictEnum.WAITING.getCode());
		int i = circleInviteDao.insert(circleInvite);
		if (i > 0) {
			pushBizzService.beInvitedCircle(beInviterId, circle.getName());
		}
		return i;
	}

	@Override
	public boolean isCircleInviteExist(Integer inviterId, Integer beInviterId, Integer circleId) {
		CircleInviteQuery circleInvite = new CircleInviteQuery();
		circleInvite.setInviterId(inviterId);
		circleInvite.setBeInviterId(beInviterId);
		circleInvite.setCircleId(circleId);
		circleInvite.setStatus(CircleInviteStatusDictEnum.WAITING.getCode());
		return circleInviteDao.queryCount(circleInvite) > 0;
	}

	@Override
	public int accept(CircleInvite circleInvite) throws BizzException {
		if (circleInvite.getStatus() != CircleInviteStatusDictEnum.WAITING.getCode()) {
			throw new BizzException("您已经处理过了这个邀请！");
		}
		if (circleMemberService.isCircleMember(circleInvite.getBeInviterId(), circleInvite.getCircleId())) {
			throw new BizzException("该用户已经是这个圈子的成员！");
		}
		int updateCount = circleMemberService.handleJoinCircle(circleInvite.getBeInviterId(),circleInvite.getCircleId(),false);
		if (updateCount > 0) {
			circleInvite.setStatus(CircleInviteStatusDictEnum.ACCEPTED.getCode());
			circleInvite.setStatusAt(TimeUtil.now());
			circleInviteDao.updateById(circleInvite);
		}
		return updateCount;
	}

	@Override
	public int ignore(CircleInvite circleInvite) throws BizzException {
		if (circleInvite.getStatus() != CircleInviteStatusDictEnum.WAITING.getCode()) {
			throw new BizzException("您已经处理过了这个邀请！");
		}
		circleInvite.setStatus(CircleInviteStatusDictEnum.IGNORED.getCode());
		circleInvite.setStatusAt(TimeUtil.now());
		return circleInviteDao.updateById(circleInvite);
	}

	@Override
	public int remove(CircleInvite circleInvite) throws BizzException {
		if (circleInvite.getStatus() != CircleInviteStatusDictEnum.WAITING.getCode()) {
			throw new BizzException("您已经处理过了这个邀请！");
		}
		
		return circleInviteDao.delete(circleInvite);
	}
}